package com.adminkk.scan;

import com.adminkk.exception.MvcException;
import com.adminkk.factory.BeanPostProcessor;
import com.adminkk.factory.MvcBeanPostProcessor;
import com.adminkk.factory.ServiceBeanPostProcessor;
import com.adminkk.handler.HandlerMapping;
import javassist.ClassClassPath;
import javassist.ClassPool;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public final  class FileScaner implements Scaner{

    public FileScaner() {
    }

    public static final List<BeanPostProcessor> beanPostProcessorList = new ArrayList<>();
    static {
        beanPostProcessorList.add(new MvcBeanPostProcessor());
        beanPostProcessorList.add(new ServiceBeanPostProcessor());
    }

    @Override
    public void doScane(String scanUrl) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
        if(scanUrl == null || scanUrl.length() == 0){
            throw new MvcException("容器基础扫描路径为空，请检查参数配置");
        }
        String baseUrl = HandlerMapping.class.getResource("/").getPath();
        String codeUrl = scanUrl.replaceAll("\\.", "/");
        String path =  baseUrl + codeUrl;
        File file = new File(path);
        if(file == null || !file.exists()){
            throw new MvcException("找不到对应扫描路径，请检查参数配置");
        }
        recursionRedFile(scanUrl,file);
    }

    //递归读取文件
    private  void recursionRedFile(String scanUrl,File file) throws MvcException, ClassNotFoundException, IllegalAccessException, InstantiationException {

        if(!file.exists()){
            return;
        }

        //读取java文件
        if(file.isFile()){

            String beanName = scanUrl.replaceAll(".class","");
            Class<?> forName = Class.forName(beanName);
            //放到Javassist容器里面
            ClassPool pool = ClassPool.getDefault();
            ClassClassPath classPath = new ClassClassPath(forName);
            pool.insertClassPath(classPath);
            if(forName.isAnnotation() || forName.isEnum() || forName.isInterface() ){
                return;
            }
            Object newInstance = forName.newInstance();

            //前置执行
            for (int i = 0; i < beanPostProcessorList.size() ; i++) {
                BeanPostProcessor beanPostProcessor = beanPostProcessorList.get(i);
                beanPostProcessor.postProcessBeforeInitialization(newInstance,beanName);
            }

            //后置执行
            for (int i = beanPostProcessorList.size()-1; i > 0  ; i--) {
                BeanPostProcessor beanPostProcessor = beanPostProcessorList.get(i);
                beanPostProcessor.postProcessAfterInitialization(newInstance,beanName);
            }
            return;
        }

        //文件夹下面的文件都递归处理
        if(file.isDirectory()){
            File[] files = file.listFiles();
            if(files != null && files.length >0){
                for (int i = 0; i < files.length; i++) {
                    File targetFile = files[i];
                    recursionRedFile(scanUrl+"."+targetFile.getName(),targetFile);
                }
            }
        }

    }
}
